package gold.utils;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

public class ConvertUtil {
    public static void main(String[] args) {
        //数组: int[]
        int[] arrayInt = new int[]{1, 2, 3, 4, 5, 6};
        printElement(arrayInt);

        //原生数组转包装类数组: int[] 2 Integer[]
        Integer[] arrayInter = Arrays.stream(arrayInt).boxed().toArray(Integer[]::new);
        printElement(arrayInter);

        //*包装类数组转List/ArrayList: Integer[] 2 List<Integer>
        List<Integer> listInter = Arrays.asList(arrayInter);
        printElement(listInter);

        //*List/ArrayList转包装类数组: List<Integer> 2 Integer[]
        Integer[] arrayInter2 = listInter.toArray(new Integer[listInter.size()]);
        printElement(arrayInter2);

        //包装类数组转原生数组: Integer[] 2 int[]
        int[] arrayInt2 = Arrays.stream(arrayInter2).mapToInt(Integer::valueOf).toArray();
        printElement(arrayInt2);

        //原生数组转List/ArrayList: int[] 2 List<Integer>
        List<Integer> listInter2 = Arrays.stream(arrayInt2).boxed().collect(Collectors.toList());
        printElement(listInter2);

        //List/ArrayList转原生数组： List<Integer> 2 int[]
        int[] arrayInt3 = listInter2.stream().mapToInt(Integer::valueOf).toArray();
        printElement(arrayInt3);
    }

    //原生数组遍历输出
    private static void printElement(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }

    //包装类数组遍历输出
    private static void printElement(Integer[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }

    //List遍历输出
    private static void printElement(List list) {
        Iterator iter = list.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }
    }

}

